package com.liusand.controller;

import com.liusand.common.Result;
import com.liusand.pojo.Category;
import com.liusand.pojo.PageInfo;
import com.liusand.service.CategoryService;
import com.liusand.vo.CategoryVo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :liusha
 * packageName :com.liusand.controller
 * fileName :CategoryController
 * creationTime :2022/11/22 10:46
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/page")
    public Result page(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Category> info = categoryService.page(page, pageSize);
        List<Category> categories = info.getRecords();
        List<CategoryVo> categoryVos = new ArrayList<>();
        for(Category c : categories){
            CategoryVo vo = new CategoryVo();
            vo.setId(c.getId());
            vo.setName(c.getName());
            vo.setType(c.getType());
            vo.setSort(c.getSort());
            vo.setCreateUser(c.getCreateUser());
            vo.setUpdateUser(c.getUpdateUser());
            LocalDateTime updateTime = c.getUpdateTime();
            vo.setUpdateTime(updateTime.toString());
            LocalDateTime createTime = c.getCreateTime();
            vo.setUpdateTime(createTime.toString());
            categoryVos.add(vo);
        }
        PageInfo<CategoryVo> pageInfo = new PageInfo<>();

        pageInfo.setRecords(categoryVos);
        pageInfo.setSize(info.getSize());
        pageInfo.setCurrent(info.getCurrent());
        pageInfo.setTotal(info.getTotal());
        return Result.success(pageInfo);
    }

    @GetMapping("/list")
    public Result list(@RequestParam("type") Integer type){
        if(type == null){
            return Result.error("获取分类列表失败");
        }
        List<Category> categories = categoryService.list(type);
        return Result.success(categories);
    }

    @PostMapping
    public Result saveCategory(@RequestBody Category category){
        if(category == null){
            return Result.error("添加分类失败");
        }
        boolean re = categoryService.saveCategory(category);
        if(!re){
            return Result.error("添加分类失败");
        }
        return Result.success("添加分类成功");
    }

    @DeleteMapping
    public Result deleteCategoryById(@RequestParam("id") String id){
        if(id == null){
            return Result.error("删除分类失败");
        }
        //todo 判断id是否存在
        boolean re = categoryService.deleteCategoryById(id);
        if(!re){
            return Result.error("删除分类失败");
        }
        return Result.success("删除分类成功");
    }

    @PutMapping
    public Result updateCategory(@RequestBody Category category){
        if(category == null){
            return Result.error("修改分类信息失败");
        }
        //todo 判断id是否存在
        boolean re = categoryService.updateCategory(category);
        if(!re){
            return Result.error("修改分类信息失败");
        }
        return Result.success("修改分类信息成功");
    }

}
